<template>
  <div class="form">
    <div class="header">
      <slot name="header"></slot>
    </div>
    <el-form :label-width="props.formConfig.labelWidth">
      <el-row>
        <template v-for="item in props.formConfig.formItem" :key="item.label">
          <el-col v-bind="colLayout">
            <el-form-item
              v-if="!item.hidden"
              :label="item.label"
              :style="{ ...formStyle }"
              :rules="item.rules"
            >
              <template v-if="item.type === 'input'">
                <el-input
                  :placeholder="item.placeholder ?? ''"
                  v-bind="item.otherOptions"
                  v-model="formData[`${item.field}`]"
                ></el-input>
              </template>
              <template v-else-if="item.type === 'select'">
                <el-select
                  style="width: 100%"
                  v-bind="item.otherOptions"
                  :placeholder="item.placeholder ?? ''"
                  v-model="formData[`${item.field}`]"
                >
                  <el-option
                    v-for="option in item.options"
                    :label="option.title"
                    :key="option.value"
                    :value="option.value"
                  />
                </el-select>
              </template>
              <template v-else-if="item.type === 'datePicker'">
                <el-date-picker
                  v-bind="item.otherOptions"
                  v-model="formData[`${item.field}`]"
                />
              </template>
            </el-form-item>
          </el-col>
        </template>
      </el-row>
    </el-form>
    <div class="footer">
      <slot name="footer">
        <div class="btns" v-if="formConfig.formBtns ?? true">
          <el-button @click="handleReset">重置</el-button>
          <el-button type="primary" @click="handleQuery">搜索</el-button>
        </div>
      </slot>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { defineProps, defineEmits, PropType, ref, watch } from 'vue'
import { IFormConfig } from './types'
const props = defineProps({
  modelValue: {
    type: Object,
    required: true
  },
  formConfig: {
    type: Object as PropType<IFormConfig>,
    default: () => ({})
  },
  colLayout: {
    type: Object,
    default: () => ({
      xl: 6,
      lg: 8,
      md: 12,
      sm: 24,
      xs: 24
    })
  },
  formStyle: {
    type: Object,
    default: () => {
      return { padding: '15px 30px' }
    }
  }
})

const emit = defineEmits(['update:modelValue', 'handleReset', 'handleQuery'])
const formData = ref({ ...props.modelValue })
watch(
  formData,
  (newValue) => {
    // console.log('----', newValue)
    emit('update:modelValue', newValue)
  },
  { deep: true }
)

const handleReset = () => {
  emit('handleReset')
}
const handleQuery = () => {
  emit('handleQuery')
}
</script>

<style scoped lang="less">
.el-form-item {
  margin-bottom: 0;
}
.btns {
  text-align: right;
  padding: 0 15px 20px;
}
</style>
